package com.xiaoniu.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoniu.forum.common.constant.ForumConstant;
import com.xiaoniu.forum.common.utils.CollectionUtils;
import com.xiaoniu.forum.dao.CommunityTopicDao;
import com.xiaoniu.forum.dao.MemberDao;
import com.xiaoniu.forum.dao.MemberFollowDao;
import com.xiaoniu.forum.entity.BannerEntity;
import com.xiaoniu.forum.entity.CommunityTopicEntity;
import com.xiaoniu.forum.entity.MemberEntity;
import com.xiaoniu.forum.entity.MemberFollowEntity;
import com.xiaoniu.forum.entity.vo.AppBaseUserVo;
import com.xiaoniu.forum.entity.vo.AppCommunityTopicVo;
import com.xiaoniu.forum.service.BannerService;
import com.xiaoniu.forum.service.IndexService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页数据服务
 */
@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private BannerService bannerService;

    @Autowired
    private CommunityTopicDao topicDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private MemberFollowDao followDao;

    @Cacheable(value = {"banners"},cacheManager = "cacheManager60S")//查询数据添加到缓存中
    @Override
    public List<BannerEntity> listBanner(int num) {
        QueryWrapper<BannerEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", ForumConstant.TRUE); //只查询状态为开启的轮播图
        Page<BannerEntity> page = new Page<>(1,num);//查询多条轮播图记录
        page.addOrder(OrderItem.asc("sort"));//根据sort值排序
        List<BannerEntity> banners = bannerService.page(page).getRecords();
        System.out.println("总共"+banners.size());
        return banners;
    }

    /***
     * 热门帖子的列表
     * 热门帖子：按帖子的浏览+评论数+点赞数算出综合分，取靠分数较高的记录
     *
     */
    @Cacheable(value = {"hotTopics"},cacheManager = "cacheManager60S")//查询数据添加到缓存中,60秒过期
    @Override
    public List<AppCommunityTopicVo> hotTopicList(Long current, Long size) {
        //获取热门帖子
        List<CommunityTopicEntity> topics = topicDao.queryHotTopics(new Page(current, size));

        //获取每个帖子对应的用户信息
        List<String> userIds = new ArrayList<>();
        for (CommunityTopicEntity topic:topics){
            userIds.add(topic.getUserId());
        }
        //如果没有数据，就不需要继续执行了，返回空数据
        if (userIds.size()==0){
            return new ArrayList<>();
        }
        List<MemberEntity> members = memberDao.selectBatchIds(userIds);
        List<AppCommunityTopicVo> topicVos = new ArrayList<>();
        topics.forEach((topic)->{
            AppCommunityTopicVo topicVo = new AppCommunityTopicVo();
            BeanUtils.copyProperties(topic,topicVo);
            topicVo.setContent(topic.getThreadsText());
            //找出帖子对应的作者
            MemberEntity user = CollectionUtils.findItem(members,(member)-> member.getId().equals(topic.getUserId()));
            topicVo.setMemberId(user.getId());
            topicVo.setMemberAvatar(user.getAvatar());
            topicVo.setMemberNickname(user.getNickname());
            topicVos.add(topicVo);
        });
        System.out.println(topicVos.size());
        return topicVos;
    }

    /**
     * 热门用户（粉丝数量较多的用户）
     */
    @Cacheable(value = {"hotUsers"},cacheManager = "cacheManager60S")//查询数据添加到缓存中,60秒过期
    @Override
    public List<AppBaseUserVo> hotUserList(Long current,Long size) {
        //先查询根据粉丝数量排序查询出一组用户的id
        List<MemberFollowEntity> follows = followDao.selectPageByFansNum(new Page(current,size));
        List<String> userIds = new ArrayList<>();
        for (MemberFollowEntity followInfo:follows){
            userIds.add(followInfo.getUserId());
        }
        //如果没有数据，就不需要继续执行了，返回空数据
        if (userIds.size()==0){
            return new ArrayList<>();
        }
        //根据这些id，查询出这些用户的信息
        List<MemberEntity> members = memberDao.selectBatchIds(userIds);
        List<AppBaseUserVo> baseUsers = new ArrayList<>();
//        members.forEach((member)->{
//            AppBaseUserVo baseUserVo = new AppBaseUserVo();
//            BeanUtils.copyProperties(member,baseUserVo);
//            baseUsers.add(baseUserVo);
//        });
        follows.forEach((follow)->{
            MemberEntity user = CollectionUtils.findItem(members,(member)-> member.getId().equals(follow.getUserId()));
            AppBaseUserVo baseUserVo = new AppBaseUserVo();
            BeanUtils.copyProperties(user,baseUserVo);
            baseUsers.add(baseUserVo);
        });
        return baseUsers;
    }
}
